//
//  ExperienceListCell.m
//  PaiLeXiongProject
//
//  Created by 小獾科技 on 2017/8/24.
//  Copyright © 2017年 小獾科技. All rights reserved.
//

#import "ExperienceListCell.h"

@interface ExperienceListCell ()
{
    dispatch_source_t _timer;
}

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *currenPriceLabel;//现价
@property (nonatomic, strong) UILabel *originalPriceLabel;//原价

@property (nonatomic, strong) UILabel *dayNumberLabel;
@property (nonatomic, strong) UILabel *dayLabel;
@property (nonatomic, strong) UILabel *hourNumberLabel;
@property (nonatomic, strong) UILabel *hourLabel;
@property (nonatomic, strong) UILabel *minuteNumberLabel;
@property (nonatomic, strong) UILabel *minuteLabel;
@property (nonatomic, strong) UILabel *secondNumberLabel;
@property (nonatomic, strong) UILabel *secondLabel;

@property (nonatomic, strong) UILabel *applyLabel;//限量

@property (nonatomic, strong) UIImageView *litleImg;
@end

@implementation ExperienceListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _imgView = [UIImageView new];
        [self addSubview:_imgView];
        [_imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).offset(10);
            make.left.equalTo(self).offset(10);
            make.width.equalTo(@113);
            make.height.equalTo(@113);
        }];
        
        _litleImg = [UIImageView new];
    
        [_imgView addSubview:_litleImg];
        [_litleImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_imgView).offset(-2);
            make.left.equalTo(_imgView).offset(9);
            make.width.equalTo(@28);
            make.height.equalTo(@14);
        }];
        
        _titleLabel = [UILabel new];
        _titleLabel.text = @"爱儿美儿童摄影秋季梦想儿童摄影秋季梦想秋季梦想";
        _titleLabel.textColor = ColorFrom16(0x323232);
        _titleLabel.font = PFRFONT(14);
        _titleLabel.numberOfLines = 2;
        [self addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(self).offset(8);
            make.top.equalTo(_imgView);
            make.left.equalTo(_imgView.mas_right).offset(10);
            make.right.equalTo(self).offset(-10);
//            make.height.height.equalTo(@40);
            make.height.greaterThanOrEqualTo(@20);
        }];
        
        _dayNumberLabel = [UILabel new];
        _dayNumberLabel.text = @"3";
        _dayNumberLabel.textColor = ColorFrom16(0xffffff);
        _dayNumberLabel.layer.backgroundColor = ColorFrom16(0xff8ca2).CGColor;
        _dayNumberLabel.layer.cornerRadius = 4;
        _dayNumberLabel.font = PFMFONT(12);
        _dayNumberLabel.textAlignment = 1;
        [self addSubview:_dayNumberLabel];
        [_dayNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_imgView.mas_right).offset(10);
            make.width.greaterThanOrEqualTo(@23);
            make.height.equalTo(@19);
        }];
        
        _dayLabel = [UILabel new];
        _dayLabel.text = @"天";
        _dayLabel.textColor = ColorFrom16(0xff8ca2);
        _dayLabel.font = PFMFONT(12);
        [self addSubview:_dayLabel];
        [_dayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_dayNumberLabel.mas_right).offset(2);
            make.width.equalTo(@13);
            make.height.equalTo(@17);
        }];
        
        _hourNumberLabel = [UILabel new];
        _hourNumberLabel.text = @"13";
        _hourNumberLabel.textColor = ColorFrom16(0xffffff);
        _hourNumberLabel.layer.backgroundColor = ColorFrom16(0xff8ca2).CGColor;
        _hourNumberLabel.layer.cornerRadius = 4;
        _hourNumberLabel.font = PFMFONT(12);
        _hourNumberLabel.textAlignment = 1;
        [self addSubview:_hourNumberLabel];
        [_hourNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_dayLabel.mas_right).offset(2);
            make.width.equalTo(@23);
            make.height.equalTo(@19);
        }];
        
        _hourLabel = [UILabel new];
        _hourLabel.text = @"小时";
        _hourLabel.textColor = ColorFrom16(0xff8ca2);
        _hourLabel.font = PFMFONT(12);
        [self addSubview:_hourLabel];
        [_hourLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_hourNumberLabel.mas_right).offset(2);
            make.width.equalTo(@26);
            make.height.equalTo(@17);
        }];
        
        _minuteNumberLabel = [UILabel new];
        _minuteNumberLabel.text = @"03";
        _minuteNumberLabel.textColor = ColorFrom16(0xffffff);
        _minuteNumberLabel.layer.backgroundColor = ColorFrom16(0xff8ca2).CGColor;
        _minuteNumberLabel.layer.cornerRadius = 4;
        _minuteNumberLabel.font = PFMFONT(12);
        _minuteNumberLabel.textAlignment = 1;
        [self addSubview:_minuteNumberLabel];
        [_minuteNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_hourLabel.mas_right).offset(2);
            make.width.equalTo(@23);
            make.height.equalTo(@19);
        }];
        
        _minuteLabel = [UILabel new];
        _minuteLabel.text = @"分";
        _minuteLabel.textColor = ColorFrom16(0xff8ca2);
        _minuteLabel.font = PFMFONT(12);
        [self addSubview:_minuteLabel];
        [_minuteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_minuteNumberLabel.mas_right).offset(2);
            make.width.equalTo(@13);
            make.height.equalTo(@17);
        }];
        
        _secondNumberLabel = [UILabel new];
        _secondNumberLabel.text = @"53";
        _secondNumberLabel.textColor = ColorFrom16(0xffffff);
        _secondNumberLabel.layer.backgroundColor = ColorFrom16(0xff8ca2).CGColor;
        _secondNumberLabel.layer.cornerRadius = 4;
        _secondNumberLabel.font = PFMFONT(12);
        _secondNumberLabel.textAlignment = 1;
        [self addSubview:_secondNumberLabel];
        [_secondNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_minuteLabel.mas_right);
            make.width.equalTo(@23);
            make.height.equalTo(@19);
        }];
        
        _secondLabel = [UILabel new];
        _secondLabel.text = @"秒";
        _secondLabel.textColor = ColorFrom16(0xff8ca2);
        _secondLabel.font = PFMFONT(12);
        [self addSubview:_secondLabel];
        [_secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_titleLabel.mas_bottom).offset(6);
            make.left.equalTo(_secondNumberLabel.mas_right).offset(2);
            make.width.equalTo(@26);
            make.height.equalTo(@17);
        }];
        
        _applyLabel = [UILabel new];
        _applyLabel.text = @"限量 20 ／申请 200人";
        _applyLabel.textColor = ColorFrom16(0x7f7e7d);
        _applyLabel.font = PFRFONT(11);
        [self addSubview:_applyLabel];
        [_applyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_dayLabel.mas_bottom).offset(3);
            make.left.equalTo(_imgView.mas_right).offset(10);
            make.width.greaterThanOrEqualTo(@145);
            make.height.equalTo(@16);
        }];
        NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:_applyLabel.text];
        NSRange range = [[aString string] rangeOfString:@"20"];
        [aString addAttribute:NSForegroundColorAttributeName value:ColorFrom16(0xFF8CA2) range:range];
        
        NSRange range1 = [[aString string] rangeOfString:@"200"];
        [aString addAttribute:NSForegroundColorAttributeName value:ColorFrom16(0xFF8CA2) range:range1];
        _applyLabel.attributedText = aString;
        
        
        
        //现价
        _currenPriceLabel = [UILabel new];
        _currenPriceLabel.text = @"￥0";
        _currenPriceLabel.textColor = ColorFrom16(0xff8ca2);
        _currenPriceLabel.font = PFMFONT(18);
        [self addSubview:_currenPriceLabel];
        [_currenPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-4);
            make.left.equalTo(_imgView.mas_right).offset(10);
            make.height.equalTo(@25);
        }];
        
        //原价
        _originalPriceLabel = [UILabel new];
        _originalPriceLabel.text = @"￥620";
        _originalPriceLabel.textColor = ColorFrom16(0x999999);
        _originalPriceLabel.font = PFRFONT(14);
        [self addSubview:_originalPriceLabel];
        [_originalPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-5);
            make.left.equalTo(_currenPriceLabel.mas_right).offset(5);
            make.height.equalTo(@20);
        }];
        
        _goToApply = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goToApply setTitle:@"立即申请" forState:0];
        [_goToApply setTitleColor:ColorFrom16(0xffffff) forState:0];
        _goToApply.titleLabel.font = PFRFONT(14);
        _goToApply.layer.backgroundColor = ColorFrom16(0xff5b7a).CGColor;
        _goToApply.layer.cornerRadius = 4;
        [self addSubview:_goToApply];
        [_goToApply mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).offset(-10);
            make.right.equalTo(self).offset(-10);
            make.width.equalTo(@74);
            make.height.equalTo(@30);
        }];
        
    }
    return self;
}

- (void)configViewWithModel:(ExperienceApplyModel *)model
{
    [_imgView sd_setImageWithURL:[NSURL URLWithString:STRING(model.goods_img)] placeholderImage:nil];
    _titleLabel.text = model.goods_name;
    _currenPriceLabel.text = [NSString stringWithFormat:@"￥%@", model.sale_price];
    _originalPriceLabel.text = [NSString stringWithFormat:@"￥%@", model.goods_price];
    _originalPriceLabel.attributedText = [GW_Tool setCutLine:_originalPriceLabel.text];
    [_litleImg sd_setImageWithURL:[NSURL URLWithString:model.label_url]];
    [self dateTimer:model];
}

- (void)dateTimer:(ExperienceApplyModel *)model
{
    //当前时间
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //HH:mm:ss
//    formatter.timeZone = [NSTimeZone localTimeZone];
//    formatter.timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    //Asia/Shanghai
    NSString *localString = [formatter stringFromDate: [NSDate date]];
    
    
    
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *endDate = [dateFormatter dateFromString:model.end_time];
    NSDate *startDate = [dateFormatter dateFromString:localString];
    
    NSDate *endDate_tomorrow = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:([endDate timeIntervalSinceReferenceDate])];
    NSTimeInterval timeInterval =[endDate_tomorrow timeIntervalSinceDate:startDate];
//    NSLog(@"截止日期 %@", endDate);
    if (_timer==nil) {
        __block int timeout = timeInterval; //倒计时时间
        @weakobj(self);
        if (timeout!=0) {
            dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
            dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
            dispatch_source_set_event_handler(_timer, ^{
                if(timeout<=0){ //倒计时结束，关闭
                    dispatch_source_cancel(_timer);//销毁定时器
                    _timer = nil;
                    dispatch_async(dispatch_get_main_queue(), ^{
                          @strongobj(self);
                        self.dayNumberLabel.text = @"";
                        self.hourNumberLabel.text = @"00";
                        self.minuteNumberLabel.text = @"00";
                        self.secondNumberLabel.text = @"00";
                    });
                }
                else{
                    int days = (int)(timeout/(3600*24));
                    if (days==0) {
                        self.dayNumberLabel.text = @"";
                    }
                    int hours = (int)((timeout-days*24*3600)/3600);
                    int minute = (int)(timeout-days*24*3600-hours*3600)/60;
                    int second = timeout-days*24*3600-hours*3600-minute*60;
                        dispatch_async(dispatch_get_main_queue(), ^{
                            
                            @strongobj(self);
                            if (days==0) {
                                self.dayNumberLabel.text = @"0";
                            }else{
                                self.dayNumberLabel.text = [NSString stringWithFormat:@"%d",days];
                            }
                            
                            if (hours<10) {
                                self.hourNumberLabel.text = [NSString stringWithFormat:@"0%d",hours];
                            }else{
                                self.hourNumberLabel.text = [NSString stringWithFormat:@"%d",hours];
                            }
                            if (minute<10) {
                                self.minuteNumberLabel.text = [NSString stringWithFormat:@"0%d",minute];
                            }else{
                                self.minuteNumberLabel.text = [NSString stringWithFormat:@"%d",minute];
                            }
                            if (second<10) {
                                self.secondNumberLabel.text = [NSString stringWithFormat:@"0%d",second];
                            }else{
                                self.secondNumberLabel.text = [NSString stringWithFormat:@"%d",second];
                            }
                            
                        });
                        
                    
                    timeout--;
                }
            });
            dispatch_resume(_timer);
        }
    }


}


- (void)dealloc
{
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
